<?php
/*
Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
include('semantic_handler.php');
/**
 * script_handler handles the tasks related to 
 * script parsing and symantic generating
 */
class script_handler
{
	private $properites;
	private $semf_root;
	private $parser_dir;
	private $parsers;
	private $symantic;
	private $project_home;
	
	/**
	 * constructor for script_handler
	 * @arg $semf_root: root directory of the SEMF
	 * @return newly created script_handler object
	 */
	public function __construct($semf_root, $project_home) {
		$this->properties = array();
		if($semf_root == NULL)
		{
			$this->semf_root = "../../";
		}
		else
		{
			$this->semf_root = $semf_root;
		}
		$this->parser_dir = $semf_root."page_designer/script/parser/";
		$this->project_home = $project_home;
		
		$this->symantic = new symantic_handler($semf_root, $project_home, "SEMF.global");
		//load parsers;
		$this->load_parsers();
	}
	
	/**
	 * parse properties individually..
	 * @param $type component type
	 * @param $id compnent id
	 * @param $component component object..
	 * @param $properties properties to parse
	 */
	public function parse_properties($type, $id, $component, $properties)
	{
		$temp_array = array($id=> array( "type"=> $type,
										"properties" => $properties,
										"component" => $component)); 
		$this->properties = array_merge($this->properties, $temp_array);
		
		$this->symantic->register_component_type($type);
	}
	
	/**
	 * generate scripts
	 * @param $result_php_file result php file name
	 * @param $result_js_file result js file name
	 */
	public function generate_script($result_dir, $result_php_file, $result_js_file,
									 $result_decl_js_file)
	{
		/*foreach($this->parsers as $parser)
		{
			$parser-> parse($this->properties);
		}*/
		$this->parsers["action_handler_parser"]-> parse($this->properties);
		$this->parsers["databinding_parser"]-> parse($this->properties);
		$this->parsers["links_parser"]-> parse($this->properties);
		
		return 
			$this->symantic->generate_symantic($result_dir, $result_php_file, $result_js_file,
										$result_decl_js_file);
	}
	
	public function load_parsers()
	{
		$this->parsers = array();
		
		$d = dir($this->parser_dir);
		// Loop through all of the files:
		$i = 0;
		while (false !== ($file = $d->read())) {
			// If the file is not this file, and does not start with a '.' or '~'
			// and end in php,
			if (($file{0} != '.') &&
				($file{0} != '..') &&
				($file{0} != '~') &&
				(substr($file, -3) == 'php')) 
			{
				include_once($this->parser_dir.$file);
				$class_name = substr($file,0, -4);
				
				//echo $class_name;
				$this->parsers[$class_name] = new ${class_name}($this->semf_root,
				                                         $this->symantic);
			}
		}
	}
}
?>